<?php
namespace app\model\cms;
use think\Model;
use think\facade\Db;
class ArticleModel extends Model{

    protected $table   = 'sh_cms_article';

    public static function get_list(  $filter = [], $select = '*' ){

        $uniacid = current_uni_account();
        $select_arr = explode(',', $select);
        $select_arr = array_map(function( $item ){
            $item = trim( $item );
            $item = 'a.' . $item;
            return $item;
        }, $select_arr);
        $select_arr_str  = implode(',',$select_arr);
        $sql = 'select '. $select_arr_str . ', c.name as category_name from sh_cms_article a '
                .' left join sh_cms_category c on a.category_id=c.id ';


        $where = [];
        $where[] = ' a.uniacid=' . $uniacid;
        if( isset($filter['status'] ) ){
            $where[] = ' a.status=' . intval( $filter['status']);
        }
        $sql .= !empty($where) ? ' where ' . implode(' and ', $where):'';
        $sql .= ' order by a.created_at desc';

        // return pdo_fetchall( $sql );
        return Db::query( $sql );
    }

    public static function single( $id ){

        $sql = 'select a.* '
            . ' from sh_cms_article a'
            . ' where a.id=' . $id;

        // return pdo_fetch( $sql );
        return Db::query( $sql )[0];
    }

    public static function detail( $id ){

        $sql = 'select a.*,c.name as category_name '
            . ' from sh_cms_article a'
            . ' left join sh_cms_category c on a.category_id=c.id'
            . ' where a.id=' . $id;

        return Db::query( $sql )[0];
    }

    public static function insert( $data ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = 'insert into sh_cms_article set '
            . ' `uniacid`=' . $uniacid
            . ' ,`title`="' . $data['title'] . '"'
            . ' ,`subtitle`="' . $data['subtitle'] . '"'
            . ' ,`category_id`=' . $data['category_id']
            . ' ,`public_time`="' . $data['public_time'] . '"'
            . ' ,`author`="' . $data['author'] . '"'
            . ' ,`content`="' . $data['content'] . '"'
            . ' ,`custom`="' . $data['custom'] . '"'
            . ' ,`out_link`="' . $data['out_link'] . '"'
            . ' ,`image`="' . $data['image'] . '"'
            . ' ,`sort_order`=' . $data['sort_order']
            . ' ,`is_recommend`=' . $data['is_recommend']
            . ' ,`viewtimes`=' . $data['viewtimes']
            . ' ,`updated_at`="' . $data['updated_at'] . '"'
            . ' ,`created_at`="' . $data['created_at'] . '"'
        ;
        // pdo_query( $sql );
        Db::execute( $sql );

        // return pdo_insertid();
        return Db::name('sh_cms_article')->getLastInsID();
    }

    public static function update2( $id, $data ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = 'update sh_cms_article set '
            . ' `uniacid`=' . $uniacid
            . ' ,`title`="' . $data['title'] . '"'
            . ' ,`subtitle`="' . $data['subtitle'] . '"'
            . ' ,`category_id`=' . $data['category_id']
            . ' ,`public_time`="' . $data['public_time'] . '"'
            . ' ,`author`="' . $data['author'] . '"'
            . ' ,`content`="' . $data['content'] . '"'
            . ' ,`custom`=\'' . $data['custom'] . '\''
            . ' ,`out_link`="' . $data['out_link'] . '"'
            . ' ,`image`="' . $data['image'] . '"'
            . ' ,`sort_order`=' . $data['sort_order']
            . ' ,`is_recommend`=' . $data['is_recommend']
            . ' ,`viewtimes`=' . $data['viewtimes']
            . ' ,`updated_at`="' . $data['updated_at'] . '"'
            . ' where id = ' . $id
        ;
// echo $sql;
        // $ret = pdo_query( $sql );
        $ret = Db::execute( $sql );
        return $ret;
    }

    public static function delete2( $id ){

        // global $_W;
        $uniacid = current_uni_account();
        $sql = "delete from sh_cms_article where "
            . " uniacid= " . $uniacid
            . " and id=" . $id;
        // return pdo_query( $sql );
        return Db::execute( $sql );
    }



}